/**
 * @author BoLiang
 */
if (typeof accedo === "undefined") {
	accedo = {};
}
if (typeof accedo.achievement === "undefined") {
	accedo.achievement = {};
}

accedo.achievement.popcn = {
		divPopcnCtn : null,
		divPopcnImg : null,
		divPopcnCont : null,
		oldOnkeydownFunc : null,
		
		init : function() {
			this.divPopcnCtn = null;
			this.divPopcnImg = null; 
			this.divPopcnCont = null;
			this.oldOnkeydownFunc = document.onkeydown;
			document.onkeydown = null;
		},
		
		createPopcnDiv : function() {
			
			this.divPopcnImg = document.createElement("div"); 
			this.divPopcnImg.id = "divPopcnImg"; 
			this.divPopcnImg.innerHTML = "<img width=100% height=100% src='http://"
								+ accedo.achievement.config.serverIP + ":" 
								+ accedo.achievement.config.serverPort + "/" 
								+ accedo.achievement.config.pluginName + "/popupPlugin/popup-images/popcnBack.png' />";
			
			this.divPopcnCtn = document.createElement("div"); 
			this.divPopcnCtn.id = "divPopcnCtn";
			this.divPopcnCtn.appendChild(this.divPopcnImg); 
			
			var bodyNode = document.getElementsByTagName("body")[0]; 
			var divArray = document.getElementsByTagName("div"); 
			for (var num = 0; num < divArray.length; num++) {
				divArray[num].style.opacity = 0.7;
			}
			bodyNode.appendChild(this.divPopcnCtn); 		
		},
		
		initPopcn : function(username, achi_sn) {
			dwr.engine.setPreHook(this.preHook); 
			dwr.engine.setPostHook(this.postHook); 
			AchiService._path = "http://"
								+ accedo.achievement.config.serverIP + ":" 
								+ accedo.achievement.config.serverPort + "/" 
								+ accedo.achievement.config.pluginName + "/dwr";
			AchiService.unlockAndJsonPopup(username, achi_sn, this.initPopcnCallback);
		},
		preHook : function() {
			
		},
		postHook : function() {
			
		},
		initPopcnCallback : function(data) {
			accedo.achievement.popcn.createPopcnDiv();
			var serverDataObj = eval("(" + data + ")");
			
//			var divStarNode = document.createElement("div");
//			divStarNode.id = "divPopupStar";
//			divStarNode.innerHTML = "<img src='http://127.0.0.1:8080/AchiPlugin/achiPlugin/achi-images/achiStar.png' />";
			
			accedo.achievement.popcn.divPopcnCont = document.createElement("div");
			accedo.achievement.popcn.divPopcnCont.id = "divPopcnCont";
			accedo.achievement.popcn.divPopcnCont.align = "center"; 
			accedo.achievement.popcn.divPopcnCont.innerHTML = "<font color='white'>ACHIEVEMENT UNLOCKED<br /> "
					+ serverDataObj.achi_name + "</font>"; 
//					+ " & YOU GOT THE CARD: "
//					+ serverDataObj.card_sn
//					+ "</font>";
//			accedo.achievement.popcn.divPopcnCtn.appendChild(divStarNode);
			accedo.achievement.popcn.divPopcnCtn.appendChild(accedo.achievement.popcn.divPopcnCont);
			
			accedo.achievement.popcn.divPopcnCtn.className = "popcnStart"; 
				
			setTimeout(function(){ 
				accedo.achievement.popcn.removePopcn();  
			},3000);
		},
		
		appendPopcn : function(username, achi_sn) {
			this.init(); 
			 
			this.initPopcn(username, achi_sn);
		},
		
		removePopcn : function() {
			
			
			var divArray = document.getElementsByTagName("div"); 
			for (var num = 0; num < divArray.length; num++) {
				divArray[num].style.opacity = 1;
			}
			
			var bodyNode = document.getElementsByTagName("body")[0]; 
			bodyNode.removeChild(this.divPopcnCtn); 
			
			document.onkeydown = this.oldOnkeydownFunc;
		}
};